package ui.maps.googlemap.drawLine
{
	import com.google.maps.LatLng;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.overlays.Polyline;
	import com.google.maps.overlays.PolylineOptions;
	import com.google.maps.styles.StrokeStyle;
	
	
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	import ui.maps.googlemap.GoogleMapService;
	
	import mx.collections.ArrayCollection;

	public class GoogleDrawLineAction
	{
		public function GoogleDrawLineAction()
		{
		}
		
		private var icoArr:ArrayCollection = new ArrayCollection();
		private var lineArr:ArrayCollection = new ArrayCollection();
		/**
		 * 设置地图MARKER图标
		 * @param mapService 地图服务对象
		 * @param _icon 图片元素
		 * @param latLng 经纬度位置
		 */ 
		public function createMapIco(mapService:GoogleMapService,_icon:DisplayObject,latLng:LatLng):void{
			var markerOptions:MarkerOptions = new MarkerOptions();
			markerOptions.iconAlignment = MarkerOptions.ALIGN_HORIZONTAL_CENTER;
			markerOptions.iconOffset = new Point(0, -_icon.height);
			//				markerOptions.iconOffset = new Point(0,0);
			markerOptions.icon = _icon;
			var marker:Marker = new Marker(latLng, markerOptions);
			mapService.addOverlay(marker);
			icoArr.addItem(marker);
		}
		
		/**
		 * 创建线路
		 * @param mapService 地图服务对象
		 * @param resultArray 轨迹点结果集合
		 */
		public function createLine(mapService:GoogleMapService,resultArray:ArrayCollection):void {
			var postionArray:Array = getlineArray(resultArray);
			var strokeStyle:StrokeStyle = new StrokeStyle({color:0x336699,alpha: 1,thickness:5});
			var polylineOption:PolylineOptions  = new PolylineOptions({strokeStyle:strokeStyle});
			//声明Polyline
			var polyline:Polyline = new Polyline(postionArray,polylineOption); 
			mapService.addOverlay(polyline);
			lineArr.addItem(polyline);
		}
		
		/**
		 * 清空所有元素
		 */ 
		public function removeAllGraphic(mapService:GoogleMapService):void
		{
			var polyline:Polyline = null;
			var marker:Marker = null;
			for(var i:int = 0; i < icoArr.length; i++){
				marker = icoArr.getItemAt(i) as Marker;
				mapService.removeOverlay(marker);
			}
			for(var i:int = 0; i <lineArr.length; i++){
				polyline = lineArr.getItemAt(i) as Polyline;
				mapService.removeOverlay(polyline);
			}
			icoArr.removeAll();
			lineArr.removeAll();
		}
		
		//转换成线路位置数组
		private function getlineArray(arrayCollection:ArrayCollection):Array{
			var offsetLineArray:Array = new Array();
			try{
				for(var index:int=0;index<arrayCollection.length;index++){
					var gpsData:Object = arrayCollection[index];
					var offsetLatLng:LatLng=new LatLng(gpsData.offsetLat,gpsData.offsetLongit);
					offsetLineArray.push(offsetLatLng);
				}
			}catch(error:Error){
				trace(error.getStackTrace());
			}
			return offsetLineArray;
		}
	}
}